package org.sardine.aop.advice;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.sardine.util.Assert;


public class AroundInterceptor implements MethodInterceptor{
	
	private Advice advice;
	
	public AroundInterceptor(Advice advice){
		Assert.notNull(advice, "AroundAdvice 不能为空");
		this.advice = advice;
	}

	public Object invoke(MethodInvocation invocation) throws Throwable {
		
		Object returnVal = null;
		
		if(advice instanceof BeforeAdvice){
			((BeforeAdvice)advice).before(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
		}
		
		returnVal = invocation.proceed();
		
		if(advice instanceof AfterAdvice){
			((AfterAdvice)advice).after(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
		}
		
		
		return returnVal;
	}

}
